#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time      :2024/12/11 14:21
# @Author    :hzy
# @FileName  :cbjq.py
import uiautomator2 as u2
import p as photo_orc
import t as text_orc
import numpy as np
import time

# python -m weditor
# python -m uiautodev

"""
{'package': 'com.mumu.launcher', 'activity': '.Launcher', 'pid': 1656}
{'package': 'com.miHoYo.hkrpg', 'activity': 'com.mihoyo.combosdk.ComboSDKActivity', 'pid': 2797}
{'package': 'com.dragonli.projectsnow.lhm', 'activity': 'com.epicgames.ue4.GameActivity', 'pid': 3405}
"""


def j_home():
    num = photo_orc.screen_find_one((58, 271), (312, 340), "./data/image_template/home.jpg", device=equipment)
    num2 = photo_orc.screen_find_one((58, 271), (312, 340), "./data/image_template/home2.jpg", device=equipment)
    if num[0] > 3 and num2[0] > 0.7:
        print(f"回到主页，置信度：{num[0] * 30}%,{num2[0] * 100}%")
        return True
    else:
        return False


def home(d):
    for i in range(20):
        d.click(1213, 37)
        time.sleep(5)
        if j_home():
            return 1
    print("回不到主页了！！！")
    return -1


def receive(d):
    im = equipment.screenshot()
    im_np = np.array(im)
    text = text_orc.ocr_get_txt_pos(im_np, "键领取")
    time.sleep(1)
    equipment.click(text[0][0][0], text[0][0][1])
    return 0


def goto_sua(d):
    d.click(852, 476)
    time.sleep(1)
    d.click(641, 550)
    time.sleep(10)
    d.click(638, 667)


if __name__ == "__main__":
    player = {}
    # time.sleep(300)
    # 连接模拟器
    equipment = u2.connect('127.0.0.1:16384')
    print("equipment获取成功")

    # 等待模拟器空闲
    t = 0
    while t < 100:
        if equipment.app_current()["package"] == "com.mumu.launcher":
            # 启动cbjq
            print("模拟器空闲，启动游戏")
            equipment.app_start("com.dragonli.projectsnow.lhm")
            time.sleep(30)
            break
        elif equipment.app_current()["package"] == 'com.dragonli.projectsnow.lhm':
            print("游戏已启动")
            break
        else:
            print("等待中......")
            time.sleep(60)
            t = t + 1
            pass
        pass

    # photo_orc.screen_find_must(A=(1140, 16), B=(1194, 70), template_way="./data/image_template/open_screen_1.jpg",device=equipment, num=6000000)

    print("# 开始游戏")
    print("游戏加载中")
    for i in range(5):
        equipment.click(645, 609)
        time.sleep(0.1)
    # 检查是否进入游戏
    # 领取月卡
    num = (0, 0, 0)
    while num[0] < 0.19:
        equipment.click(1241, 612)
        time.sleep(1)
        num = photo_orc.screen_find_one(A=(1181, 9), B=(1269, 73), template_way="./data/image_template/set.jpg",
                                        device=equipment)
        pass

    print("# 收取好友体力")
    # 点击好友
    equipment.click(273, 307)
    time.sleep(3)
    # 收取体力
    for i in range(5):
        equipment.click(1171, 682)
        time.sleep(1)
    # 回到主页
    home(d=equipment)
    time.sleep(10)
    # 购买每日体力
    equipment.click(117, 382)  # 进入供应站
    time.sleep(5)
    equipment.click(44, 374)  # 打开补给箱
    time.sleep(5)
    # 判断是否免费
    # 检测文字
    im = equipment.screenshot()
    im_np = np.array(im)
    text = text_orc.ocr_get_txt_pos(im_np, "每日物资补给")
    print(text)
    if text[0]:
        print("购买体力")
        equipment.click(258, 356)  # 选择每日补给
        time.sleep(5)
        equipment.click(896, 560)  # 购买
    else:
        print("已经购买")
    time.sleep(5)
    # 回到主页
    home(d=equipment)
    time.sleep(10)
    
    print("# 进入活动")
    equipment.click(883, 312)
    time.sleep(5)
    print("进入刷体力")
    im = equipment.screenshot()
    im_np = np.array(im)
    text = text_orc.ocr_get_txt_pos(im_np, "材料")
    time.sleep(1)
    equipment.click(text[0][0][0], text[0][0][1]-40)
    time.sleep(5)
    print("选择关卡")
    im = equipment.screenshot()
    im_np = np.array(im)
    text = text_orc.ocr_get_txt_pos(im_np, "深")
    n = 0
    while not text[0]:  # 判断是否
        im = equipment.screenshot()
        im_np = np.array(im)
        text = text_orc.ocr_get_txt_pos(im_np, "深")
        n = n+1
        if n >= 10:
            print("出错了！！！")
            break
        else:
            print(f"第{n}次尝试失败")
    time.sleep(1)
    equipment.click(text[0][0][0], text[0][0][1])
    time.sleep(5)
    im = equipment.screenshot()
    im_np = np.array(im)
    text = text_orc.ocr_get_txt_pos(im_np, "/240")
    now_Value = int(text[1][0].split("/")[0])
    run_times = now_Value//30
    surplus_Value = now_Value%30
    go_times = run_times//9 + 1
    player["tili_Value"] = surplus_Value
    print(f"目前有{now_Value}体力,将运行{run_times}次，{go_times}轮，剩余{surplus_Value}体力,预计{(240-surplus_Value)*6}分钟")
    time.sleep(1)
    for i in range(go_times):
        equipment.click(920, 658)
        time.sleep(1)
        goto_sua(d=equipment)
        time.sleep(10)
    home(d=equipment)
    time.sleep(10)

    print("# 领取活动任务")
    print("进入活动")
    equipment.click(883, 312)
    time.sleep(5)
    print("进入任务")
    im = equipment.screenshot()
    im_np = np.array(im)
    text = text_orc.ocr_get_txt_pos(im_np, "任务")
    time.sleep(1)
    equipment.click(text[0][0][0], text[0][0][1] - 10)
    time.sleep(5)
    receive(d=equipment)
    time.sleep(5)
    home(d=equipment)
    time.sleep(10)

    print("# 每日购买")
    equipment.click(1168, 677)
    time.sleep(5)
    equipment.swipe_ext("up",0.5)
    time.sleep(1)
    equipment.swipe_ext("up", 0.5)
    time.sleep(5)
    im = equipment.screenshot()
    im_np = np.array(im)
    text = text_orc.ocr_get_txt_pos(im_np, "中级职级认证")
    time.sleep(1)
    equipment.click(text[0][0][0], text[0][0][1])
    time.sleep(5)
    equipment.click(1145, 670)
    time.sleep(5)
    home(d=equipment)
    time.sleep(10)

    print("# 刷取角色碎片")
    equipment.click(1081, 309)
    time.sleep(5)
    equipment.click(559, 564)
    time.sleep(5)
    equipment.click(126, 604)
    time.sleep(2)
    goto_sua(d=equipment)
    equipment.click(636, 20)
    time.sleep(2)
    equipment.click(376, 598)
    time.sleep(5)
    goto_sua(d=equipment)
    time.sleep(5)
    home(d=equipment)
    time.sleep(10)

    print("# 领取每日委托")
    equipment.click(926, 206)
    time.sleep(5)
    receive(d=equipment)
    time.sleep(10)
    home(d=equipment)
    time.sleep(10)

    print("# 领取通行证")
    equipment.click(264, 384)
    time.sleep(5)
    equipment.click	(755, 683)
    time.sleep(5)
    receive(d=equipment)
    time.sleep(10)
    home(d=equipment)





